package com.qdlc.p2p.biz.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.BaseGiveAwardService;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.dao.AdditionalDrawLotteryRuleDao;
import com.qdlc.p2p.dal.dao.AdditionalUserDrawLotteryDao;
import com.qdlc.p2p.dal.dao.NewActivityAwardLogDao;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dao.UserSignDao;
import com.qdlc.p2p.dal.dto.AdditionalDrawLotteryRule;
import com.qdlc.p2p.dal.dto.AdditionalUserDrawLottery;
import com.qdlc.p2p.dal.dto.NewActivityAwardLog;
import com.qdlc.p2p.dal.model.BaseGiveAwardModel;

/**
 * Created by liaoxiangkai on 2015/11/12.
 */
@Service("giveUserLotteryService")
public class GiveUserLotteryServiceImpl implements BaseGiveAwardService {
    private static Logger logger = Logger.getLogger(GiveUserLotteryServiceImpl.class);
    @Resource
    UserDao userDao;
    @Resource
    AdditionalDrawLotteryRuleDao additionalDrawLotteryRuleDao;
    @Resource
    AdditionalUserDrawLotteryDao additionalUserDrawLotteryDao;
    @Resource
    UserSignDao userSignDao;
    @Resource
    NewActivityAwardLogDao newActivityAwardLogDao;

    @Override
    public void giveAward(BaseGiveAwardModel baseGiveAwardModel) {
        AdditionalUserDrawLottery additionalUserDrawLottery = new AdditionalUserDrawLottery();
        // 判断规则存在.判断状态 0:启用 1:未启用
        AdditionalDrawLotteryRule additionalDrawLotteryRule = additionalDrawLotteryRuleDao.find(baseGiveAwardModel.getRuleId());
        if (additionalDrawLotteryRule == null || additionalDrawLotteryRule.getStatus() != 0) {
            logger.info("发放抽奖奖励异常，规则未找到。additionalDrawLotteryRule的ID：" + baseGiveAwardModel.getRuleId());
            return;
        }
        Date startTime = DateUtil.getTodayFirstIntegralTime();
        Date endTime = DateUtil.rollDayStart(startTime, additionalDrawLotteryRule.getValidDay());

        //赠送抽奖
        additionalUserDrawLottery.setCount(additionalDrawLotteryRule.getCount());
        additionalUserDrawLottery.setUser(baseGiveAwardModel.getUser());
        additionalUserDrawLottery.setAdditionalDrawLotteryRule(additionalDrawLotteryRule);
        additionalUserDrawLottery.setStartTime(startTime);
        additionalUserDrawLottery.setEndTime(endTime);
        additionalUserDrawLottery.setSourceType(baseGiveAwardModel.getSourceType());
        additionalUserDrawLottery.setDescription(baseGiveAwardModel.getDescription());
        additionalUserDrawLottery.setAddTime(DateUtil.getNow());
        additionalUserDrawLottery = additionalUserDrawLotteryDao.save(additionalUserDrawLottery);
        if (additionalUserDrawLottery != null) {
            //赠与活动记录
            NewActivityAwardLog newActivityAwardLog = new NewActivityAwardLog();
            newActivityAwardLog.setUser(baseGiveAwardModel.getUser());
            newActivityAwardLog.setTache(baseGiveAwardModel.getSourceType());
            newActivityAwardLog.setRuleType(baseGiveAwardModel.getRuleType());
            newActivityAwardLog.setRuleId(baseGiveAwardModel.getRuleId());
            newActivityAwardLog.setCount(additionalDrawLotteryRule.getCount());
            newActivityAwardLog.setAddTime(DateUtil.getNow());
            newActivityAwardLogDao.save(newActivityAwardLog);
        }
    }

    /**
     * 发放给单个用户
     *
     * @param baseGiveAwardModel
     */
    @Override
    public void giveSingleUserAward(BaseGiveAwardModel baseGiveAwardModel) {
        AdditionalUserDrawLottery additionalUserDrawLottery = new AdditionalUserDrawLottery();
        additionalUserDrawLottery.setCount(((Double)baseGiveAwardModel.getAmount()).intValue());
        additionalUserDrawLottery.setUser(baseGiveAwardModel.getUser());
        additionalUserDrawLottery.setStartTime(DateUtil.getTodayFirstIntegralTime());
        additionalUserDrawLottery.setEndTime(DateUtil.rollDayStart(DateUtil.getTodayFirstIntegralTime(), baseGiveAwardModel.getValidDay()));
        additionalUserDrawLottery.setSourceType(baseGiveAwardModel.getSourceType());
        additionalUserDrawLottery.setDescription(baseGiveAwardModel.getDescription());
        additionalUserDrawLottery.setAddTime(DateUtil.getNow());
        additionalUserDrawLottery = additionalUserDrawLotteryDao.save(additionalUserDrawLottery);
        if (null != additionalUserDrawLottery) {
            NewActivityAwardLog newActivityAwardLog = new NewActivityAwardLog(baseGiveAwardModel.getUser(), baseGiveAwardModel.getSourceType(), baseGiveAwardModel.getAmount(), BaseGiveAwardModel.RULE_TYPE_LOTTERY);
            newActivityAwardLogDao.save(newActivityAwardLog);
        }
    }
}
